using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrackSystem.IO.Data;
using TrackSystem.IO.Models;

namespace TrackSystem.IO.Protocol
{
    /// <summary>
    /// GPVVTG 通信协议 
    /// 0--航向;
    /// 1--水平运动角度（Knot）;
    /// 2--水平运动角度（km/h）;
    /// </summary>
    public class Ship2GPVTGProtocol : ProtocolBase
    {
        private int startTicks;
        private bool bSendR;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public Ship2GPVTGProtocol(string name)
            : base()
        {
            NeedInit = false;
            Name = name;
            bSendR = false;
        }

        /// <summary>
        /// 生成读取寄存器的通讯帧
        /// </summary>
        /// <returns></returns>
        public override byte[] GenerateReadFrame(out int NeedRecvLen)
        {
            startTicks = Environment.TickCount;
            if (bSendR)
            {
                Fail_R++;
                // 失败超过10次，成功次数清零
                if (Fail_R >= 10)
                {
                    Succ_R = 0;
                }
            }
            bSendR = true;
            NeedRecvLen = 40;
            return null;
        }


        /// <summary>
        /// 根据收到的内容进行处理
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public override bool ProcessReadFrame(byte[] buffer)
        {
            var DataFactory = RegFactory.Instance;
            Lapse = Environment.TickCount - startTicks;
            try
            {
                string frame = Encoding.UTF8.GetString(buffer);
                string[] ar_line = frame.Split('\n');
                try
                {
                    for (int i = 0; i < ar_line.Length; i++)
                    {
                        string[] ar_str = ar_line[i].Split(',');
                        int strLen = ar_str.Length;

                        if (strLen == 0)   // 数据长度为0
                        {
                            continue;
                        }
                        Rx_r = buffer;  // 用于显示的接收帧内容
                        // $GPVTG,< 1 >,< 2 >,N,< 3 >,K*hh
                        if (strLen == 6 && ar_str[0] == "$GPVTG")
                        {
                            double d1 = double.Parse(ar_str[1]); // 真北运动角度
                            double d2 = double.Parse(ar_str[2]); // 水平运动速度（knot）
                            double d3 = double.Parse(ar_str[4]); // 水平运动速度（km/h）
                            DataFactory.SetVal(RegNameList[0], d1);
                            DataFactory.SetVal(RegNameList[1], d2);
                            DataFactory.SetVal(RegNameList[2], d3);
                            continue;
                        }
                    }
                }
                catch (Exception)
                {
                    return false;
                }
                Succ_R++;
                // 失败次数清零
                if (Succ_R > 30 && Fail_R > 0)
                {
                    Fail_R = 0;
                }
                bSendR = false;
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}
